<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="电影名称"
          clearable
          size="small"
          style="width: 200px"
        ></el-input>
      </el-form-item>
      <el-form-item prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="用户电话"
          clearable
          size="small"
          style="width: 200px"
        ></el-input>
      </el-form-item>
      <el-form-item prop="date">
        <el-date-picker
          v-model="queryParams.date"
          type="datetime"
          placeholder="选择日期时间"
          value-format="yyyy-MM-dd HH:mm:ss"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        :show-overflow-tooltip="true"
        prop="id"
        label="订单编号"
        sortable
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="name"
        label="电影名称"
        width="180"
        align="center"
      ></el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="time"
        label="提交时间"
        width="180"
        align="center"
      ></el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="phone"
        label="用户电话"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="价格(数量)" width="90" align="center">
        <template slot-scope="scope">{{scope.row.price}}¥({{scope.row.num}})</template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="座位" width="90" align="center">
        <template slot-scope="scope">{{scope.row.rws}}排{{scope.row.clmn}}座</template>
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="mdate"
        label="放映时间"
        width="180"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" align="center" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row,scope.$index)"
          >查看</el-button>
          <el-button
            @click="handleDelete(scope.row)"
            icon="el-icon-delete"
            size="mini"
            type="text"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getMovieList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-row :gutter="15">
        <el-form ref="formData" :model="formData" :rules="rules" size="medium" label-width="100px">
          <el-col :span="12">
            <el-form-item label="ID" prop="id">
              <el-input
                v-model="formData.id"
                placeholder="系统自动生成"
                :maxlength="11"
                show-word-limit
                :disabled="true"
                clearable
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型" prop="type">
              <el-select
                v-model="formData.type"
                placeholder="请选择类型"
                clearable
                :style="{width: '100%'}"
              >
                <el-option
                  v-for="(item, index) in typeOptions"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发布状态" prop="active">
              <el-select
                v-model="formData.active"
                placeholder="请选择发布状态"
                clearable
                :style="{width: '100%'}"
              >
                <el-option
                  v-for="(item, index) in statusOptions"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="地址" prop="url">
              <el-input
                v-model="formData.url"
                placeholder="请输入地址"
                clearable
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrders } from "@/api/orders/index";

export default {
  name: "Swiper",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 表格数据
      ordersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [
        {
          value: "1",
          label: "发布"
        },
        {
          value: "0",
          label: "未发布"
        }
      ],
      typeOptions: [
        {
          value: "image",
          label: "图片"
        },
        {
          value: "video",
          label: "视频"
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        phone: undefined,
        date: undefined
      },
      // 表单参数
      formData: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getOrdersList();
  },
  methods: {
    /** 查询列表 */
    getOrdersList() {
      this.loading = true;
      listOrders(this.queryParams).then(response => {
        this.ordersList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 首页展示修改
    handleShowChange(row) {
      updateShow(row.id, row.show).then(res => {
        this.msgSuccess("修改成功");
      });
    },
    // 取消按钮
    close() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.formData = {
        id: undefined,
        type: undefined,
        active: undefined,
        url: undefined
      };
      this.resetForm("formData");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getOrdersList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getSwiperById(row.id).then(res => {
        this.formData = res.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    handelConfirm: function() {
      this.$refs["formData"].validate(valid => {
        if (valid) {
          if (this.formData.id != undefined) {
            updateItem(this.formData).then(res => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getSwiperList();
            });
          } else {
            addItem(this.formData).then(res => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getSwiperList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.id || this.ids;
      this.$confirm('是否确认删除编号为"' + Ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return deleteItem(Ids);
        })
        .then(() => {
          this.getSwiperList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    }
  }
};
</script>